/*   
 * Copyright (c) 2010-2020 Founder Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * Founder. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Founder.   
 *   
 */
package com.wujw.auto.generate.impl;

import java.io.File;
import java.util.List;

import com.wujw.auto.bean.ColumnStruct;
import com.wujw.auto.bean.TableStruct;
import com.wujw.auto.generate.GenerateBean;
import com.wujw.auto.generate.GetTableDao;
import com.wujw.auto.utils.ConfigUtil;
import com.wujw.auto.utils.DataTypeUtil;
import com.wujw.auto.utils.FileUtil;
import com.wujw.auto.utils.NameUtil;

/**
 * This class is used for ...
 * 
 * @author wujw
 * @version 1.0, 2017年11月11日
 */
public class GenerateBeanImpl implements GenerateBean {

    GetTableDao getTableDao = new GetTablesDaoImpl();
    List<TableStruct> list = getTableDao.getTablesStruct();

    @Override
    public boolean createBean() {
        /* =======获取配置文件的参数===== */
        // 项目路径
        String projectPath = ConfigUtil.projectPath;
        // 是否生成bean实体
        boolean beanFlag = ConfigUtil.beanFlag;
        // bean 包名
        String beanPackage = ConfigUtil.beanPackage;

        if (beanFlag) {
            // 包名由com.xxx.xxx 替换成com/xxx/xxx
            String beanPath = beanPackage.replace(".", File.separator);
            // bean实体的路径
            String path = projectPath + File.separator+"src"+File.separator+"main"+File.separator+"java"+File.separator + beanPath;

            // 遍历所有表结构
            for (int i = 0; i < list.size(); i++) {
                // 文件名
                String fileName = NameUtil.fileName(list.get(i).getTableName());
                System.out.println(fileName + " >>>>>>>>> bean开始生成");

                // 列结构
                List<ColumnStruct> columns = list.get(i).getColumns();
                /* =======拼接所有内容 ============== */
                String packageContent = "package " + beanPackage + ";\n\n";
                StringBuffer importContent = new StringBuffer();
                String className = "public class " + fileName + " {\n\n";
                StringBuffer classContent = new StringBuffer();
                StringBuffer getterContent = new StringBuffer();
                StringBuffer setterContent = new StringBuffer();

                // 遍历list 将字段名和字段的类型写入文件
                for (int j = 0; j < columns.size(); j++) {
                    // 属性名
                    String columnName = NameUtil.columnName(columns.get(j).getColumnName());
                    // 数据类型
                    String type = columns.get(j).getDateType();
                    //列名注释
                    String columnContent =columns.get(j).getColumnContent();
                    // 转化
                    String dataType = DataTypeUtil.getType(type);
                    // 部分类型还需要导包
                    if ("Date".equals(dataType)) {
                        importContent.append("import java.util.Date;\n\n");
                    }
                    if ("Timestamp".equals(dataType)) {
                        importContent.append("import java.sql.Timestamp;\n\n");
                    }

                    // 生成属性
                    classContent.append("\t" + "private" + "\t" + dataType + "\t" + columnName + ";\t//"
                    +(columnContent==null?"":columnContent)+"\n");
                    // set/get方法名采取"get/set"+ 列名首字母大写
                    String setgetName = columnName.substring(0, 1).toUpperCase() + columnName.substring(1);
                    // 生成get方法
                    getterContent.append("\t" + "public" + "\t" + dataType + "\t" + "get" + setgetName + "(){\n"
                            + "\t\t" + "return" + "\t" + columnName + ";\n" + "\t" + "}\n");
                    // 生成set方法
                    setterContent.append("\t" + "public void" + "\t" + "set" + setgetName + "(" + dataType + " "
                            + columnName + "){\n" + "\t\t" + "this." + columnName + " = " + columnName + ";\n" + "\t"
                            + "}\n");

                }
                // 拼接实体
                StringBuffer content = new StringBuffer();
                content.append(packageContent);
                content.append(importContent.toString());
                content.append(className);
                content.append(classContent.toString());
                content.append(setterContent.toString());
                content.append(getterContent.toString());
                content.append("}");
                FileUtil.createFileAtPath(path + File.separator, fileName + ".java", content.toString());
                System.out.println(fileName + " >>>>>>>>> bean生成完毕");
            }
            return true;
        }

        return false;
    }

}
